package cn.mesmile.flow.listen;

import cn.hutool.core.date.DateUtil;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.springframework.stereotype.Component;

/**
 * @author zb
 * @date 2020/12/8 10:32
 * @Description  自定义事件监听器 【自定义事件监听器】
 * */
@Component
public class MyEventListener implements FlowableEventListener {


    /**
     * {@link FlowableEngineEventType}
     *
     * @param event
     */
    @Override
    public void onEvent(FlowableEvent event) {

        System.out.println(">>>>>>>>>>>>>>>>   进入自定义的监听器   <<<<<<<<<<<<<<<<<<<");
        System.out.println(">>>>>>>>>>>>>>>>   进入自定义的监听器   <<<<<<<<<<<<<<<<<<<");
        System.out.println(">>>>>>>>>>>>>>>>   进入自定义的监听器   <<<<<<<<<<<<<<<<<<<");

        System.out.println("onEvent 》》》》》》》》》 DateUtil.now() = " + DateUtil.now());

        if(event.getType() == FlowableEngineEventType.JOB_EXECUTION_SUCCESS) {
            System.out.println("A job well done!");
        } else if (event.getType() == FlowableEngineEventType.JOB_EXECUTION_FAILURE) {
            System.out.println("A job has failed...");
        } else {
            // FlowableEngineEventType  流程的类型
            System.out.println(">>>>>>>>> Event received: " + event.getType());
        }

    }

    @Override
    public boolean isFailOnException() {

        // isFailOnException（）方法确定在调度事件时onEvent（..）方法引发异常时的行为。
        // 返回false时，将忽略该异常。

        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        // isFireOnTransactionLifecycleEvent（）方法确定在事件发生时或在getOnTransaction（）方法
        // 确定的事务生命周期事件中立即触发此事件侦听器。事务生命周期事件的支持值为：
        // COMMITTED，ROLLED_BACK，COMMITTING，ROLLINGBACK

        return false;
    }

    @Override
    public String getOnTransaction() {

        System.out.println("getOnTransaction 》》》》》》》》 DateUtil.now() = " + DateUtil.now());

        return null;
    }
}
